<?php
/**
 * Author：晴天 QQ：1186258278
 * Creation：2020/4/22 13:31
 * Filename：kayix.php
 * 卡易信
 */

namespace lib\supply;


use Curl\Curl;
use extend\kamun;

class kayix
{
    /**
     * @param $OrderData 订单信息
     * @param $Goods 商品信息
     * @param $TypeSupply 对接货源信息
     * 提交下单信息！
     */
    public static function Submit($OrderData, $Goods, $TypeSupply)
    {
        global $accredit, $conf;
        $inputs = explode('|', $Goods['value']);
        $DataPost = [
            'username' => $TypeSupply['username'],
            'password' => $TypeSupply['password'],
            'url' => $TypeSupply['url'],
            'cache' => 1,
            'encrypt' => 1,
            'token' => $accredit['token'],
            'domain' => href(),
            'goodsId' => (float)$Goods['goods_id'],
            'mainKey' => $Goods['goods_mainKey'],
            'sumprice' => $OrderData['num'],
            'saleprice' => (float)$Goods['money'] * $OrderData['num'],
            'salePwd' => $TypeSupply['secret'],
            'goods_url' => $Goods['goods_url'],
            'data' => [
                'input' => $inputs,
                'value' => json_decode($OrderData['input'], TRUE),
            ]
        ];

        if ($TypeSupply['pattern'] == 1) {
            include_once ROOT . 'includes/extend/Share.php';
            include_once ROOT . 'includes/extend/kamun.php';
            $DataCurl = kamun::GoodsPurchased($DataPost);
        } else {
            $DataCurl = Curl::curl(false, ['data' => xiaochu_en(json_encode($DataPost))], true, '/Kamun/goods_order', 2);
        }

        if (!isset($DataCurl['code'])) {
            $Msg = '下单信息获取失败!,请检查对接配置！';
            $code = $conf['SubmitState'];
            $money = 0;
            $order = -1;
        } else {
            $Msg = $DataCurl['msg'];
            $code = ($DataCurl['code'] == 1 ? 1 : $conf['SubmitState']);
            $money = ($DataCurl['code'] == 1 ? $DataCurl['money'] : 0);
            $order = ($DataCurl['code'] == 1 ? $DataCurl['order'] : -1);
        }

        return [
            'code' => $code,
            'msg' => $Msg,
            'money' => $money,
            'order' => $order,
        ];
    }

    /**
     * @param $id 订单号
     * @param $TypeSupply 对接货源信息
     */
    public static function Query($id, $TypeSupply)
    {
        $DataPost = [
            'username' => $TypeSupply['username'],
            'password' => $TypeSupply['password'],
            'url' => $TypeSupply['url'],
            'cache' => 1,
            'order' => $id,
        ];

        if ($TypeSupply['pattern'] == 1) {
            include_once ROOT . 'includes/extend/Share.php';
            include_once ROOT . 'includes/extend/kamun.php';
            $DataCurl = kamun::OrderQuery($DataPost);
        } else {
            $DataCurl = Api::Curl(false, $DataPost, true, '/Kamun/query', 2);
            $DataCurl = json_decode($DataCurl, TRUE);
        }
        if ($DataCurl == null || $DataCurl['code'] <> 1) return false;
        $Data = $DataCurl['data'];

        $StateArr = explode('&nbsp;  ', $Data['schedule']);
        $Initial = explode('：', $StateArr[0])[1];
        $Present = explode('：', $StateArr[2])[1];

        return [
            'ApiType' => 1,
            'ApiNum' => $Data['num'],
            'ApiTime' => $Data['addtime'],
            'ApiInitial' => $Initial,
            'ApiPresent' => $Present,
            'ApiState' => $Data['state'],
            'ApiError' => ($Data['result'] == '' ? '无' : $Data['result']),
        ];
    }

}